package com.deyinet.bdcms.controller;

import com.alibaba.fastjson.JSON;
import com.deyinet.bdcms.controller.dtoandapi.RedisDeviceApi;
import com.deyinet.bdcms.entity.Device;
import com.deyinet.bdcms.entity.Sysconf;
import com.deyinet.bdcms.entity.Syslog;
import com.deyinet.bdcms.entity.Sysmodule;
import com.deyinet.bdcms.service.IDeviceService;
import com.deyinet.bdcms.service.ILogsService;
import com.deyinet.bdcms.service.IsysConfService;
import com.deyinet.bdcms.tools.DeviceToRedisObj;
import com.deyinet.bdcms.tools.RedisPoolUtils;
import com.deyinet.bdcms.tools.WebUtilify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/22 0022.
 */
@Controller
@RequestMapping(value = "/sysmanage/sys")
public class SysConfig {
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private IsysConfService sysConfService;
    @Autowired
    private ILogsService logsService;
    @RequestMapping(value = "/sysconfiglist")
    public void sysconfiglist() {
    }

    @RequestMapping(value = "/mapCenterConf")
    @ResponseBody
    public Map<String, Object> mapCenterConf(String location, HttpSession session,HttpServletRequest request) {
        ServletContext application=session.getServletContext();
        Sysconf conf=sysConfService.get(1);
        conf.setConfValue(location);
        String[] locations = location.split(",");
        application.setAttribute("centerLng",locations[0]);
        application.setAttribute("centerLat",locations[1]);
        Map<String, Object> info = new HashMap<>();
        Syslog log = new Syslog();
        log.setTitle("修改地图中心位置");//日志内容
        log.setContent("修改地图中心的经纬度为："+location);
        Sysmodule sysmodule=new Sysmodule();
        sysmodule.setId(13);//根据数据库sysmodule表的对应的id去set
        log.setSysmoduleByModuleid(sysmodule);
        log.setSysuserByUserid(WebUtilify.LoginUserinfo(request));
        log.setCreatedate(new Timestamp(System.currentTimeMillis()));
        WebUtilify.WriteLog(request,log, logsService);

        info.put("content", "操作成功");
        info.put("Message", "ok");
        return info;
    }



    @RequestMapping(value = "/initializeredis", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> initializeredis() {
        Map<String, Object> info = new HashMap<>();

        int isSuccess = updataAllToRedis();
        if (isSuccess == 0) {
            info.put("message", "操作失败");
            info.put("content", "ok");
        } else {
            info.put("message", "操作成功");
            info.put("content", "ok");
        }
        return info;
    }


    public int updataAllToRedis() {
        try {
            List<Device> list = deviceService.getList();
            Jedis redis = RedisPoolUtils.getJedisObject();
            Pipeline p = redis.pipelined();
            Map<String, String> data = new HashMap<>();
            for (int i = 0; i < list.size(); i++) {
                Device device = list.get(i);
                RedisDeviceApi api=new RedisDeviceApi();
                data.put(device.getSn(), JSON.toJSONString(DeviceToRedisObj.transform(device,api)));
            }
            p.hmset(RedisPoolUtils.dbname, data);
            p.sync();
            p.close();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
